<?php

class Application_Model_Dao_TypeCredit extends Zend_Db_Table_Abstract
{

	protected $_name = 'type_credits';
	protected $_primary = 'id';

	public function init(){
		 
	}
	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
		throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");
		return $result;
	}
public function getAllTypeCredit(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('type_credits');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
public function getRowTypeCredit($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('type_credits')->where('type_credits.id = \''.$id.'\'');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	public function getTypeCreditByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
		throw new Exception("Rodzaj zaliczenia o ID $id nie istnieje.");
		return $row->toArray();
	}
	public function getTypeCreditByDesignation($designation){
		$row = $this->fetchRow('designation = '.$designation);
		if (!$row)
		throw new Exception("Rodzaj zaliczenia o nazwie $designation nie istnieje.");
		return $row->toArray();
	}
	public function saveTypeCredit($typeCredit) {
		$data = array(
			'designation' => $typeCredit->getDesignation(),
		);
		$this->insert($data);
		return ;
	}
	public function updateTypeCredit($typeCredit) {
		$data = array(
			'designation' => $typeCredit->getDesignation(),
		);
		$this->update($data, 'id = '. (int)$typeCredit->getId());
		return ;
	}
public function deleteTypeCredit($typeCredit){
		$this->delete('id =' . (int)$typeCredit->getId());
		return;
	}
	public function getToSelect(){
		$array = $this->getAll()->toArray();
		$typeCredits[''] = "wybierz";
		foreach ($array as $option) {
			$typeCredits[ $option['id'] ] = $option['designation' ] ;
		}
		return $typeCredits;
	}

}
 